<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@ include file="/jsp/common/common.jsp"%>  

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<title></title>
		<meta http-equiv="pragma" content="no-cache">
        <meta http-equiv="cache-control" content="no-cache">
        <meta http-equiv="expires" content="0">
		<link rel="stylesheet" type="text/css" href="<c:url value="/static/jquery-easyui/themes/default/easyui.css"/>">
		<link rel="stylesheet" type="text/css"
			href="<c:url value="/static/jquery-easyui/themes/icon.css"/>">
		<script type="text/javascript"
			src="<c:url value="/static/jquery-easyui/jquery-1.6.min.js"/>"></script>
		<script type="text/javascript"
			src="<c:url value="/static/jquery-easyui/jquery.easyui.min.js"/>"></script>
		<script type="text/javascript"
			src="<c:url value="/static/jquery-easyui/plugins/jquery.panel.js"/>"></script>
		<script type="text/javascript"
			src="<c:url value="/static/jquery-easyui/plugins/jquery.datagrid.js"/>"></script>
		<script type="text/javascript"
			src="<c:url value="/static/jquery-easyui/plugins/jquery.messager.js"/>"></script>
		<script type="text/javascript"
			src="<c:url value="/static/jquery-easyui/ext-for-framework.js.jsp"/>"></script>
		<script type="text/javascript" src="<c:url value="/static/jquery-easyui/locale/easyui-lang-zh_CN.js"/>"></script>	
		<script type="text/javascript"
			src="<c:url value="/static/js/common.js"/>"></script>		
		<script>
		
			var map = {}; // 存取编辑框内信息的map

			function initList(listDisplayEId) {
				$('#' + listDisplayEId).datagrid(
						{
							nowrap : false,
							striped : true,
							collapsible : true,
							remoteSort : true,
							notSelectByRow : false,
							/* frozenColumns : [ [ {
								field : 'ids',
								checkbox : true
							} ] ], */
							
							
							/* onDblClickRow: function (rowIndex, rowData) {  
								$('#listDisplay').datagrid('beginEdit', rowIndex);
							}, */
							 
							toolbar : [
									{
										id : listDisplayEId + '_btn_edit',
										text : '提交',
										iconCls : 'icon-edit',
										handler : function() {	
											
											var rows = $('#'+listDisplayEId).datagrid('getSelections');	
											for(var i=0; i<rows.length; i++){
											    var rowIndex=$('#'+listDisplayEId).datagrid('getRowIndex',rows[i]['foodId']);
												$('#listDisplay').datagrid('endEdit', rowIndex);
											}
											var row = $('#'+listDisplayEId).datagrid('getSelections');	
											if(row.length == 0){
												$.messager.alert('提示','请选择一条记录操作！','info');
												return;
											}else{
												var userList = new Array(); 
												var jsonFater;
												for (var i = 0 ; i < row.length;i++){
													if ( row[i]['foodNum'] == "" || !isNumberAndNoZero(row[i]['foodNum'])  ){
														alert("菜品"+row[i]['foodName']+"，份数请填写数字！");
														return false;
													}
													var  jsonSun = "{foodId:'"+row[i]['foodId']+"',foodNum:'"+row[i]['foodNum']+"'}";
													userList.push(jsonSun);
												}
												jsonFater = JSON.stringify(userList);
												var str = "json="+jsonFater;	
												document.getElementsByName("fooddategridList")[0].value = jsonFater;
												$.messager.confirm('提示', '是否提交记录？', function(r){
													//方法函数 进入后台
													if(r){
														addFood();
														$('#'+listDisplayEId).datagrid('reload');
														$('#'+listDisplayEId).datagrid('clearSelections');
													}
												});
											}
										}
									}],
							onLoadError:function(){
								$.messager.alert('错误','获取数据失败，请与管理员联系！','error');
							},
							
							onLoadSuccess:function(){ 
								var rows = $('#'+listDisplayEId).datagrid('getRows');	
								if(rows.length > 0){

									for (var i = 0 ; i < rows.length;i++){

										var key = rows[i]['foodId'];
										var has = key in map; 
										if(has){
											rows[i]['foodNum'] = map[key];
											$('#listDisplay').datagrid('refreshRow', i);
										}
									}
								}
							
				            	$(".datagrid-row").mouseover(function(e){  
				            		if(checkHover(e,this)){
				            			var foodId = $(this).find('td').eq(1).text();
					        			var index = $('#listDisplay').datagrid('getRowIndex', foodId);
						        		$('#listDisplay').datagrid('beginEdit',index);
					                }
				                });   
				        		$(".datagrid-row").mouseout(function(e){  
				        			if(checkHover(e,this)){
				        				var foodId2 = $(this).find('td').eq(1).text();
				        				var index2 = $('#listDisplay').datagrid('getRowIndex', foodId2);
				        				$('#listDisplay').datagrid('endEdit',index2);
				        				
				        				if( $(this).find('td').eq(9).text()!=''){
					        				map[$(this).find('td').eq(1).text()] = $(this).find('td').eq(9).text(); 
				        				}else{
				        					delete map[$(this).find('td').eq(1).text()];
				        				}
				        			}
				        		});   
				        	} 
						 ,onClickRow:function(rowIndex){
								$(".myrad").prop("checked",false);
								$(".myrad").eq(rowIndex).prop("checked",true);
							}  
						});
				
			};
			
			$(document).ready(function() {
				initList('listDisplay');
				//根据日期生成星期
				/* $("input",$("#orderDate").next("span")).bind("propertychange", function() { 
					var arr=this.value.split("-"); 
					if(arr.length != 3){
						arr=this.value.split(",");
					}
					var date=new Date(parseInt(arr[0]),parseInt(arr[1])-1,parseInt(arr[2]));  
					var today = new Array('星期日','星期一','星期二','星期三','星期四','星期五','星期六');  
					$("#orderDay").val(today[date.getDay()]);
				});   */
				
			});
 			
 			function addFood(){
 	 			$('#AddFoodForm').form('submit',
 				{
 					url:'<c:url value='/order/commitOrder.do'/>', //查询提交路径
 					onSubmit:function()//提交按钮相应函数
 					{
 						return $('#AddFoodForm').form('validate');
 					},
 					success:function(data){
 						data = eval( "("+data+")" );
 		       			if(data.success){
 		       				$.messager.alert("提示", "提交成功！", 'info');
 	       				}else{
 	       					var message = "";
        					if(data.msg.indexOf("msg001") >= 0 ){
        						var arrayMsg =  data.msg.split("|");
        						message = "id为"+arrayMsg[1]+"的材料，您的供货商没有设定请联系采购部门。";
        					}else if(data.msg == "msg002"){
                                message = "选择菜品的时间已过，请联系采购部。";
                            }
 	       					$.messager.alert("提示", message, 'warning');
 	       				}
 					},
 					error:function(){
 						$.messager.alert("错误", "系统错误，请联系管理员！", 'error');
 						return;
 					}
 				});
 	 			
 			}
 			
 			//下拉框
			$(function() {        
		        // 下拉框选择控件，下拉框的内容是动态查询数据库信息  
		        $('#foodType1').combobox({   
		            data:[{"foodType1":"0","ordStateName":"全部"},{"foodType1":"1","ordStateName":"牛肉类"},{"foodType1":"2","ordStateName":"海鲜类 "},{"foodType1":"3","ordStateName":"水发类"},{"foodType1":"4","ordStateName":"猪肉类"},{"foodType1":"5","ordStateName":"浇头"},{"foodType1":"6","ordStateName":"鸭肉类"},{"foodType1":"7","ordStateName":"鸡肉类"},{"foodType1":"8","ordStateName":"鸡蛋类"},{"foodType1":"9","ordStateName":"鹌鹑类"},{"foodType1":"10","ordStateName":"河鲜类"},{"foodType1":"11","ordStateName":"豆制品类"},{"foodType1":"12","ordStateName":"根茎类"},{"foodType1":"13","ordStateName":"鸭蛋类"},{"foodType1":"14","ordStateName":"干货类"},{"foodType1":"15","ordStateName":"绿叶菜"},{"foodType1":"16","ordStateName":"瓜果类"},{"foodType1":"17","ordStateName":"叶菜"},{"foodType1":"18","ordStateName":"其他类"},{"foodType1":"19","ordStateName":"猪骨类"},{"foodType1":"20","ordStateName":"蛋类"},{"foodType1":"21","ordStateName":"酸辣"},{"foodType1":"22","ordStateName":"咸菜类"},{"foodType1":"23","ordStateName":"鱼头类"},{"foodType1":"24","ordStateName":"猪血"}],   
		            editable:false, //不可编辑状态  
		            cache: false,  
		            valueField:'foodType1',     
		            textField:'ordStateName',  
		        });      
		    });
			function searchInfo(){
				if($('#foodType1').combobox('getValue') ==''){
					$('#listDisplay').datagrid('load', {
						foodType1 : 0,
						foodName : $('#foodName').val(),
						foodMtype : $('#foodMtype').val(),
						foodMname : $('#foodMname').val()
					}); 
				}else{
					$('#listDisplay').datagrid('load', {
						foodType1 : $('#foodType1').combobox('getValue'),
						foodName : $('#foodName').val(),
						foodMtype : $('#foodMtype').val(),
						foodMname : $('#foodMname').val()
					});  
				}
			}
		</script>
			</head>
			<body>
			<form id="AddFoodForm" name="AddFoodForm" method="post">
			<table width="98%">
			<tr><td>
			<div style="height: auto; width:1180px;background: #fafafa; padding: 2px;" class="easyui-panel" title="&nbsp&nbsp&nbsp&nbsp&nbsp必录信息"  collapsible="true" minimizable="false" maximizable="false" closable="false">
			   <table cellpadding="5">
			      <tr>
                  	<td>菜品使用日期</td>
                    <td>
 			            <input class="easyui-datebox" name="orderDate" id="orderDate" editable="false" data-options="required:true"/> 
 			        </td>
                  </tr>
                  <tr>
                    <td>
				        <input class="easyui-validatebox" type="hidden" name="fooddategridList"  />
 			        </td>
                  </tr>
               </table>
            </div>
            </td></tr>
            <tr><td>
			<div style="height: auto; width:1180px;background: #fafafa; padding: 5px;" class="easyui-panel" title="查询" iconCls="icon-search" collapsible="true" minimizable="false" maximizable="false" closable="false">
					<form id="searchForm" name="searchForm" method="post"
						action="userlist.txt"
						onsubmit="return searchDGData(this.id,'listDisplay');">
						<div>
		            	<table cellpadding="5">
								<tr>
									<td>菜名：</td>
									<td><input class="easyui-validatebox" id="foodName" editable="true" name="foodName"></input></td>
									<td>类别：</td>
									<td><input class="easyui-combobox" id="foodType1" editable="false" name="foodType1"></input></td>
									<td>主料大全：</td>
									<td><input class="easyui-validatebox" id="foodMtype" editable="true" name="foodMtype"></input></td>
									<td>主料半成品名称：</td>
									<td><input class="easyui-validatebox" id="foodMname" editable="true" name="foodMname"></input></td>
									
									
		                    		<td><input type="button" onclick="searchInfo()" value="查询"></td>
		                    	</tr>
		                </table>					 
						</div>
					</form>
				</div>
			</td></tr>
            </table>
		<table width="98%">
		
		<tr style="height: 12px;"></tr>
		<tr>
		<td>
				<table id="listDisplay" title="菜品列表" 
			iconCls="icon-list" singleSelect="false" idField="foodId"
			url="<c:url value="/food/findFood.do"/>" loadMsg="请稍候......"
			pagination="true" pageSize="20"
			pageList="[10,20,30,40,50,100]" sortName="foodId"
			rownumbers="true" sortOrder="asc">
			<thead>
				<tr>
				    <th field="foodRole" width="25" align="center"  checkbox="true">
					</th> 
				    <th field="foodId" width="80"  sortable="true" >
						菜品ID
					</th>	
 					<th field="foodName" width="80" sortable="true" ">
						菜名
					</th>		
					<th field="foodValue" width="100" align="right" formatter="formatDGField" sortable="true" >
						价格
					</th>
					<th field="foodType" width="60" formatter="formatFoodType" sortable="true" >
						品别
					</th>	
					<th field="foodType1" width="60" formatter="formatFoodType1" sortable="true"  >
						类别
					</th>	
					<th field="foodMtype" width="60" formatter="formatDGField" sortable="true" >
						主料大全
					</th>	
					<th field="foodMname" width="60" formatter="formatDGField" sortable="true" >
						主料半成品名称
					</th>	
					<th field="foodMater" width="260" formatter="formatDGField" sortable="true" >
						所需材料
					</th>	
					<th field="foodNum" width="50" sortable="true" editor="text"  >
						份数
					</th>	
 				</tr>
			</thead>
			</table>
			</td>
			</tr>
		</table>
		</form>
	</body>
</html>